/**
 * 
 */
package com.ccjq.shosestore.shoppingcart.dao.impl;

import com.ccjq.shosestore.core.dbmanager.DbConnection;
import com.ccjq.shosestore.core.utils.UuidUtils;
import com.ccjq.shosestore.shoppingcart.dao.ShoppingCartDao;
import com.ccjq.shosestore.shoppingcart.model.SaveCartModel;
import com.ccjq.shosestore.shoppingcart.model.ShowCartModel;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 继承增删改的抽象类   实现接口ShoppingCartDao
 * 
 * @author 李佳鑫
 */
public class ShoppingCartDaoImpl implements ShoppingCartDao {

	Connection con = null;
	PreparedStatement pre = null;
	ResultSet rs = null;
	
	public int addCart(SaveCartModel saveCartModel) {
		//得到向购物车中插入商品的信息
		String shoppingCartId = UuidUtils.generateShortUUID("S", 10);
		String productId = saveCartModel.getProductId();
		int quantity = saveCartModel.getQuantity();
		String userId = saveCartModel.getUserId();
		int row = 0;
		//当数量商品的数量大于0，才可以插入
		if(quantity > 0){
			//查询该用户是否添加过该商品，如果添加过就在原数量的的基础上进行添加，否则就新建购物车信息
			String cartId = queryProductById(productId,userId);
			if(cartId.length() > 0){
				System.out.println(cartId);
				//说明购物车中之前存在该商品  在原有基础上进行修改添加
				String sql = "update t_shopping_cart set quantity = quantity + '"+quantity+"' where shopping_cart_id = '"+cartId+"'";
				try {
					row = pre.executeUpdate(sql);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}else{
				System.out.println("购物车不存在该商品");
				String sql = "insert into T_SHOPPING_CART values(?,?,?,?)";
				con = DbConnection.getConnection();
				try{
					pre = con.prepareStatement(sql);
					pre.setString(1,shoppingCartId);
					pre.setString(2,productId);
					pre.setInt(3,quantity);
					pre.setString(4,userId);
					row = pre.executeUpdate();
				}catch(SQLException e){
					e.printStackTrace();
				}
			}
		}
		return row;
	}
	
	public List<ShowCartModel> queryCartById(String userId) {
		List<ShowCartModel> cartsList = new ArrayList<ShowCartModel>();
		String sql = "select imgpath,description,quantity,price,quantity*price,s.SHOPPING_CART_ID,s.PRODUCT_ID "
				+ "from T_PRODUCT p join T_SHOPPING_CART s on p.PRODUCT_ID = s.PRODUCT_ID "
				+ "where s.USER_ID = ?";
		con = DbConnection.getConnection();
		try{
			pre = con.prepareStatement(sql);
			pre.setString(1,userId);
			rs = pre.executeQuery();
			while(rs.next()){
				ShowCartModel showCartModel = new ShowCartModel();
				showCartModel.setImgpath(rs.getString(1));
				showCartModel.setDescription(rs.getString(2));
				showCartModel.setQuantity(rs.getInt(3));
				showCartModel.setPrice(rs.getDouble(4));
				showCartModel.setTotal(rs.getDouble(5));
				showCartModel.setShoppingCartId(rs.getString(6));
				showCartModel.setProductId(rs.getString(7));
				cartsList.add(showCartModel);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DbConnection.close(rs, pre, con);
		}
		return cartsList;
	}

	public int delCartById(String shoppingCartId){
		int row = 0;
		String sql = "delete from T_SHOPPING_CART where SHOPPING_CART_ID = ?";
		con = DbConnection.getConnection();
		try{
			pre = con.prepareStatement(sql);
			pre.setString(1,shoppingCartId);
			row = pre.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}
		return row;
	}

	public int updateCartById(int quantity, String shoppingCartId) {
		int row = 0;
		String sql = "update T_SHOPPING_CART set QUANTITY = ? where SHOPPING_CART_ID = ?";
		con = DbConnection.getConnection();
		try{
			pre = con.prepareStatement(sql);
			pre.setInt(1,quantity);
			pre.setString(2,shoppingCartId);
			row = pre.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}
		return row;
	}

	public int queryStockById(String productId) {
		System.out.println(productId);
		int stock = 0;
		String sql = "select quantity from t_stock where product_id = ?";
		con = DbConnection.getConnection();
		try{
			pre = con.prepareStatement(sql);
			pre.setString(1,productId);
			rs = pre.executeQuery();
			while(rs.next()){
				System.out.println("库存量是="+rs.getInt(1));
				stock = rs.getInt(1);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return stock;
	}

	public int updateStockById(int quantity,String productId) throws SQLException{
		int row = 0;
		String sql = "update t_stock set quantity = (select quantity from t_stock where product_id = ?) - ? where product_id = ?";
		con = DbConnection.getConnection();
		pre = con.prepareStatement(sql);
		pre.setString(1,productId);
		pre.setInt(2,quantity);
		pre.setString(3, productId);
		row = pre.executeUpdate();
		return row;
	}

	public int emptyCart(String userId) throws SQLException{
		int row = 0;
		String sql = "delete from t_shopping_cart where user_id = ?";
		con = DbConnection.getConnection();
		pre = con.prepareStatement(sql);
		pre.setString(1,userId);
		row = pre.executeUpdate();
		System.out.println("购物车清理成功");
		return row;
	}

	public double queryTotal(String userId) {
		double total = 0;
		String sql = "select sum(quantity * price) from (select price, quantity from t_shopping_cart a join t_product b on a.product_id = b.product_id where user_id = ?)";
		con = DbConnection.getConnection();
		try{
			pre = con.prepareStatement(sql);
			pre.setString(1,userId);
			rs = pre.executeQuery();
			while(rs.next()){
				System.out.println("购物车商品总价是="+rs.getInt(1));
				total = rs.getInt(1);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return total;
	}

	public String queryProductById(String productId,String userId) {
		String cartId = "";
		String sql = "select shopping_cart_id from t_shopping_cart where product_id = ? and user_id = ?";
		con = DbConnection.getConnection();
		try{
			pre = con.prepareStatement(sql);
			pre.setString(1,productId);
			pre.setString(2, userId);
			rs = pre.executeQuery();
			while(rs.next()){
				System.out.println("存在的商品="+rs.getString(1));
				cartId = rs.getString(1);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return cartId;
	}
}
